package com.itlong.cloud.app.service.impl;

import com.itlong.cloud.POJO.PO.AppTempFaceShowPO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.app.dao.IAppTempFaceShowDao;
import com.itlong.cloud.app.service.IAppShowService;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 *      演示版App服务层接口实现类
 * </desc>
 *
 * @createDate 2018/3/6
 */
@Service
public class AppShowServiceImpl implements IAppShowService {

    @Autowired
    private IAppTempFaceShowDao iAppTempFaceShowDao;

    /**
     * <desc>
     *      根据userId查询余额
     * <desc/>
     * @param  userId   带查询的userId
     * @return 余额
     * @author Biao.R
     * @createDate 2018/3/6
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public BigDecimal getBalanceByUserId(String userId) {
        BigDecimal accountMoney = BigDecimal.ZERO;
        if (StringUtils.isEmpty(userId)) {
            return accountMoney;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        AppTempFaceShowPO faceShowPO = iAppTempFaceShowDao.getBalanceByParams(params);
        if (faceShowPO != null) {
            accountMoney = faceShowPO.getAccountMoney();
        }
        return accountMoney;
    }

    /**
     * <desc>
     *      人脸识别演示向账户充值或扣费
     * <desc/>
     * @param   faceShowPO  人脸识别PO
     * @return  人脸识别PO
     * @author Biao.R
     * @createDate 2018/3/6
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Map<String, Object> updateBalance(AppTempFaceShowPO faceShowPO) {
        try {
            String userId = faceShowPO.getUserId();
            BigDecimal accountMoney = faceShowPO.getAccountMoney();
            String faceId = faceShowPO.getFaceId();
            Map<String, Object> returnMap = new HashMap<>();
            //userId和faceId必须存在一个
            if ((StringUtils.isEmpty(userId) && StringUtils.isEmpty(faceId)) || accountMoney == null) {
                return null;
            }
            Map<String, Object> params = new HashMap<>();
            params.put("userId", userId);
            params.put("faceId", faceId);
            faceShowPO = iAppTempFaceShowDao.getBalanceByParams(params);
            if (faceShowPO == null) {
                returnMap.put("status", 2);
                return returnMap;
            }
            BigDecimal latestBalance = faceShowPO.getAccountMoney().add(accountMoney);
            //比较余额是不是充足
            if (latestBalance.compareTo(BigDecimal.ZERO) == -1) {
                returnMap.put("status", 1);
                return returnMap;
            }
            faceShowPO.setAccountMoney(latestBalance);
            faceShowPO.setCreateTime(null);
            faceShowPO.setUpdateTime(new Date());
            iAppTempFaceShowDao.update(SqlUtil.durableData(faceShowPO, PlatformConstants.TABLE_UPDATE));
            returnMap.put("status", 0);
            returnMap.put("balance", latestBalance);
            return returnMap;
        } catch (Exception e) {
            throw new DataAccessException("【演示版App_AppShowServiceImpl】_updateBalance更新账户余额失败", e);
        }
    }
}